#include <malloc.h>
#include "linked_list.h"

bool CreateHead(__in node * head){
	head = (node*)malloc(sizeof(node));
	if ( head == NULL)
		return false;
	return true;
}

bool InsertNode(__in node * head,__in char word){
	if ( head == NULL )
		return false;
	if ( head->next == NULL ){
		node * p1 = (node*)malloc(sizeof(node));
		if( p1 == NULL )
			return false;
		p1->word = word;
		p1->next = NULL;
		head->next = p1;
		return true;
	}
	while ( head->next != NULL )
		head = head->next;
	node * p1 = (node*)malloc(sizeof(node));
	if ( p1 == NULL )
		return false;
	p1->word = word;
	p1->next = NULL;
	head->next = p1;
	return true;
}

bool InsertNodeAfter(__in node * head,__in char keyword,char word){
	if ( head == NULL )
		return false;
	if ( head->next == NULL )
		return InsertNode ( head,word);
	while ( head->word != keyword ){
		head = head->next;
		if ( head->next == NULL )
			return false;
	}
	node * p1 = (node*)malloc(sizeof(node));
	if ( p1 == NULL)
		return false;
	p1->word = word;
	p1->next = head->next;
	head->next = p1;
	return true;
}

bool InsertNodeBefore(__in node * head,__in char keyword,char word){
	if ( head == NULL )
		return false;
	if ( head->next == NULL )
		InsertNode(head,word);
	while ( head->next->word != keyword ){
		head = head->next;
		if ( head->next == NULL )
			return false;
	}
	node * p1 = (node*)malloc(sizeof(node));
	if ( p1 == NULL )
		return false;
	p1->word = word;
	p1->next = head->next;
	head->next = p1;
	return true;
}

bool DestroyNode(__in node * head,__in char word){
	if ( head == NULL )
		return false;
	if ( head->next == NULL )
		return false;
	while ( head->next->word != word ){
		head = head->next;
		if ( head->next == NULL )
			return false;
	}
	head->next = head->next->next;
	return true;
}


